<template>
  <!-- 添加或修改项目开票信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="80%">
    <el-form :model="form" :rules="rules" label-width="120px" ref="form">
      <el-tabs class="detail-tab" type="card" v-model="activeName">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <!--<el-col :span="12">
              <el-form-item label="项目id" prop="projectId">
                <el-input v-model="form.projectId" placeholder="请输入项目id"/>
              </el-form-item>
            </el-col>-->
            <el-col :span="24">
              <el-form-item label="项目名称" prop="projectName">
                <el-input disabled placeholder="请输入项目名称" v-model="form.projectName"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发票代码" prop="preNo">
                <el-input placeholder="请输入发票编号" v-model="form.preNo"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发票编号" prop="taxNo">
                <el-input placeholder="请输入发票编号" v-model="form.taxNo"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票金额" prop="invoiceMoney">
                <el-input-number :max="form.invoiceMoney" :precision="2" placeholder="请输入开票金额"
                                 v-model="form.invoiceMoney"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票日期" prop="invoiceDate">
                <el-date-picker clearable
                                placeholder="请选择开票日期"
                                type="date"
                                v-model="form.invoiceDate"
                                value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注信息" prop="infomation">
                <el-input placeholder="请输入内容" type="textarea" v-model="form.infomation"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <project-invoice-info-list-for-prj-form :projectId="form.projectId" :projectName="form.name"
                                              ref="projectInvoiceInfoListForPrjForm"></project-invoice-info-list-for-prj-form>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button @click="submitForm" type="primary">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {addAction, getById, updateAction} from '@/api/manage'
    import ProjectInvoiceInfoListForPrjForm from "./ProjectInvoiceInfoListForPrjForm";

    export default {
        name: "ProjectInvoiceInfoFormForPrj",
        dicts: ['fund_callback'],
        components: {
            ProjectInvoiceInfoListForPrjForm,
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    preNo: [
                        {required: true, message: "发票代码不能为空", trigger: "blur"}
                    ],
                    taxNo: [
                        {required: true, message: "发票编号不能为空", trigger: "blur"}
                    ],
                    invoiceMoney: [
                        {required: true, message: "发票编号不能为空", trigger: "blur"}
                    ],
                },
                url: {
                    getById: '/projectInvoiceInfo/projectInvoiceInfo/',
                    add: '/projectInvoiceInfo/projectInvoiceInfo/add',
                    edit: '/projectInvoiceInfo/projectInvoiceInfo/edit',
                },
            };
        },
        created() {

        },
        methods: {
            handleAddNew(data) {
                let that = this;
                that.reset();
                that.open = true;
                that.form.projectId = data.id;
                that.form.projectName = data.name;
                that.form.invoiceMoney = data.taxRemain;
                // that.companyName = data.companyName;
                that.title = "添加项目发票信息";

                that.$refs.projectInvoiceInfoListForPrjForm.getData(data);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null,
                    invoiceMoney: null,
                    invoiceDate: new Date(),
                    infomation: null,
                    projectId: null,
                    projectName: null,
                    taxNo: null,
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加项目开票信息";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改项目开票信息";
                });
            },
            /** 提交按钮 */
            submitForm() {
                let that = this;
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        that.form.backMoney = 0;
                        that.form.remainMoney = that.form.invoiceMoney;
                        that.form.status = 0;
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.add, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },
        }
    };
</script>
